package com.zhsm.fuxi.mvp.ui.activitys

import android.Manifest
import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.os.Handler
import android.provider.Settings
import com.hankkin.reading.base.BaseMvpActivity
import com.hankkin.reading.control.UserControl
import com.tbruyelle.rxpermissions2.RxPermissions
import com.zhsm.fuxi.R
import com.zhsm.fuxi.bean.UserBean
import com.zhsm.fuxi.mvp.contract.SplashContract
import com.zhsm.fuxi.mvp.presenter.SplashPresenter
import com.zhsm.fuxi.mvp.ui.dialog.HintDialog

class SplashActivity : BaseMvpActivity<SplashPresenter>(), SplashContract.IView {
    val beginTime = System.currentTimeMillis()
    val mHintDialog: HintDialog by lazy { HintDialog(this) }
    override fun isSupportSwipeBack(): Boolean {
        return false
    }

    override fun registerPresenter() = SplashPresenter::class.java

    override fun getLayoutId(): Int {
        return R.layout.activity_splash
    }

    override fun initView() {
        setMiuiStatusBar()
    }

    override fun onResume() {
        super.onResume()
        getPermissions()
    }

    override fun getUserInfoFail() {
        startMain()
    }

    override fun saveUserInfo(userBean: UserBean?) {
        userBean?.let {
            UserControl.setCurrentUser(it)
        }
        startMain()
    }

    private fun startMain() {
        val time = 1500 - (System.currentTimeMillis() - beginTime)
        if (time > 100) {
            Handler().postDelayed({
                startActivity(Intent(this, MainActivity::class.java))
                finish()
            }, time)
        } else {
            startActivity(Intent(this, MainActivity::class.java))
            finish()
        }
    }

    // 请求权限
    @SuppressLint("CheckResult")
    fun getPermissions() {
        RxPermissions(this).request(
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION)
                .subscribe { aBoolean ->
                    if (aBoolean!!) {
                        if (UserControl.isLogin()) {
                            getPresenter().getUserInfo()
                        } else {
                            Handler().postDelayed({
                                startActivity(Intent(this, LoginActivity::class.java))
                                finish()
                            }, 1500)
                        }
                    } else {
                        showMissingPermissionDialog()
                    }
                }
    }

    // 显示缺失权限提示
    fun showMissingPermissionDialog() {
        mHintDialog.setOnClickAndRes(resources.getString(R.string.basic_help), R.drawable.att_out_hint, true, "设置",
                object : HintDialog.onClickBtn {
                    override fun cancelBtn() {
                        mHintDialog.dismiss()
                        finish()
                    }

                    override fun sureBtn() {
                        startAppSettings()
                        mHintDialog.dismiss()
                    }
                })
        mHintDialog.show()
    }

    // 启动应用的设置
    fun startAppSettings() {
        val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
        intent.data = Uri.parse("package:$packageName")
        startActivity(intent)
    }
}
